//package com.lengyu.kangdeli.config;
//
//import com.lengyu.kangdeli.schedules.ComputerRoomMonitorSchedule;
//import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.integration.annotation.ServiceActivator;
//import org.springframework.integration.channel.DirectChannel;
//import org.springframework.integration.core.MessageProducer;
//import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
//import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
//import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
//import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
//import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
//import org.springframework.messaging.Message;
//import org.springframework.messaging.MessageChannel;
//import org.springframework.messaging.MessageHandler;
//import org.springframework.messaging.MessagingException;
//
//@Configuration
//public class MqttConfig {
//    private final static Logger logger = LoggerFactory.getLogger(MqttConfig.class);
//    /**
//     * 连接mqtt服务器的工厂
//     * @return
//     */
//    @Bean
//    public MqttPahoClientFactory mqttClientFactory() {
//        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
//        MqttConnectOptions options = new MqttConnectOptions();
//        options.setServerURIs(new String[] { "tcp://172.2.0.28:1883" });
//        options.setUserName("spring_guest");
//        options.setPassword("spring_guest".toCharArray());
//        factory.setConnectionOptions(options);
//        return factory;
//    }
//
//    @Bean
//    public MessageChannel mqttOutboundChannel() {
//        return new DirectChannel();
//    }
//
//    @Bean
//    @ServiceActivator(inputChannel = "mqttOutboundChannel") //inputChannel的名字必须和上面的MessageChannel的方法名保持一致
//    public MessageHandler mqttOutbound() {
//        MqttPahoMessageHandler messageHandler =
//                new MqttPahoMessageHandler("spring_monitor", mqttClientFactory());
//        messageHandler.setAsync(true);
//        messageHandler.setDefaultTopic("r/ups/I01227005795");
//        return messageHandler;
//    }
//
//    /**
//     * 收消息的通道，注意实际开发中和发的可能不在一起
//     * @return
//     */
//    @Bean
//    public MessageChannel mqttInputChannel() {
//        return new DirectChannel();
//    }
//
//    @Bean
//    public MessageProducer inbound() {
//        MqttPahoMessageDrivenChannelAdapter adapter =
//                new MqttPahoMessageDrivenChannelAdapter("tcp://172.2.0.28:1883", "spring_monitor",
//                        "r/ups/I01227005795");
//        adapter.setCompletionTimeout(5000);
//        adapter.setConverter(new DefaultPahoMessageConverter());
//        adapter.setQos(1);
//        adapter.setOutputChannel(mqttInputChannel());
//        return adapter;
//    }
//
//    /**
//     * 收消息的处理器，用于如何处理消息
//     * mqttInputChannel 代表的是收消息的通道对象的id
//     * @return
//     */
//    @Bean
//    @ServiceActivator(inputChannel = "mqttInputChannel")
//    public MessageHandler handler() {
//        return new MessageHandler() {
//
//            @Override
//            public void handleMessage(Message<?> message) throws MessagingException {
//                ComputerRoomMonitorSchedule.UNProcessMonitorList.offer(message.getPayload().toString());
//
//                logger.info(message.getPayload().toString());
//            }
//
//        };
//    }
//
//}
